﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Deeper.Common;
using Deeper.Common.Web;
using Deeper.DataAccess;
using ZhongTian.Model;
using ZhongTian.Services;


namespace ZhongTian.Web
{
	public partial class CustomerInfo : Page
	{
		private string OrderBy { get { return Customer.F.CustomerId.Desc.ToString(); } }

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				rptList.ReBind();
			}
		}


		protected PageResult MyDataBind(int pageIndex, int pageSize)
		{
			string key = string.Format("%{0}%", txtKeyWord.Text);
			WhereClip where = (Customer.F.CustName.Like(key) | Customer.F.Py.Like(key));
			//MessageBox.Show(where.ToFormatsString());
			return new DataContext().GetPageList<Customer>(pageIndex, pageSize, where: where, orderby: OrderBy);
		}


		protected void ModelAdd()
		{
			pnlDetail.GetEdit(new Customer());
		}

		protected void ModelSave()
		{
			"保存".InfoAction(() =>
			{
				int id = hfCustomerId.Value.ToInt();
				Customer model = id == 0 ? new Customer() : new DataContext().GetEntityById<Customer>(id);
				model.GetData(pnlDetail);
				if (model.ItemPrice.IsNull()) model.ItemPrice = "";
				model.Py = model.CustName.GetPyCode();
				model.Save();

				pnlDetail.Hide();
				rptList.ReBind();
			});
		}


		protected void rptList_Command(object sender, RepeaterCommandEventArgs e)
		{
			int id = e.CommandArgument.ToString().ToInt();
			string cmd = e.CommandName.ToLower();
			switch (cmd)
			{
				case "delete":
					"删除".InfoAction(() =>
					{
						new DataContext().Delete<Customer>(id);
						rptList.ReBind();
					});
					break;
				case "edit":
					"获取编辑信息".InfoAction(() =>
					  {
						  pnlDetail.GetEdit(new DataContext().GetEntityById<Customer>(id));
					  }, false);
					break;
			}

		}

		protected void btnSearch_Click(object sender, EventArgs e)
		{
			rptList.ReBind();
		}
	}


}